package org.crystal.tradecraft.model.binance.enumeration;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.crystal.tradecraft.exception.NoIntervalEnumException;

import java.time.Duration;

@Getter
@RequiredArgsConstructor
public enum Interval {
    ONE_SECOND("1s", Duration.ofSeconds(1)),
    ONE_MINUTE("1m", Duration.ofMinutes(1)),
    THREE_MINUTES("3m", Duration.ofMinutes(3)),
    FIVE_MINUTES("5m", Duration.ofMinutes(5)),
    FIFTEEN_MINUTES("15m", Duration.ofMinutes(15)),
    THIRTY_MINUTES("30m", Duration.ofMinutes(30)),
    ONE_HOUR("1h", Duration.ofHours(1)),
    TWO_HOURS("2h", Duration.ofHours(2)),
    FOUR_HOURS("4h", Duration.ofHours(4)),
    SIX_HOURS("6h", Duration.ofHours(6)),
    EIGHT_HOURS("8h", Duration.ofHours(8)),
    TWELVE_HOURS("12h", Duration.ofHours(12)),
    ONE_DAY("1d", Duration.ofDays(1)),
    THREE_DAYS("3d", Duration.ofDays(3)),
    ONE_WEEK("1w", Duration.ofDays(7)),
    ONE_MONTH("1M", Duration.ofDays(30));

    private final String expression;

    private final Duration duration;

    public static Interval getIntervalEnum(String intervalStr) {
        
        for (Interval interval : Interval.values()) {
            if (interval.getExpression().equalsIgnoreCase(intervalStr)) {
                return interval;
            }
        }
        throw new NoIntervalEnumException("Cannot find the corresponding enum for the interval %s".formatted(intervalStr));
    }
}
